package com.ruoyi.business.service.impl;

import java.util.List;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.business.domain.TGoodsSpecs;
import com.ruoyi.business.mapper.TGoodsSpecsMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TGoodsMapper;
import com.ruoyi.business.domain.TGoods;
import com.ruoyi.business.service.ITGoodsService;

/**
 * 商家商品管理Service业务层处理
 *
 * @author 陈捷
 * @date 2024-11-21
 */
@Service
public class TGoodsServiceImpl implements ITGoodsService
{
    @Autowired
    private TGoodsMapper tGoodsMapper;

    @Autowired
    private TGoodsSpecsMapper tGoodsSpecsMapper;

    /**
     * 查询商品详情
     *
     * @param goodsId 商品id
     * @return 商品
     */
    @Override
    public TGoods selectTGoodsByGoodsId(Long goodsId)
    {
        TGoods tGoods = tGoodsMapper.selectTGoodsByGoodsId(goodsId);
        if (tGoods!=null){
            List<TGoodsSpecs> tGoodsSpecs = tGoodsSpecsMapper.selectTGoodsSpecsByGoodsId(goodsId);
            tGoods.settGoodsSpecsList(tGoodsSpecs);
        }
        return tGoods;
    }

    /**
     * 查询商家商品管理列表
     *
     * @param tGoods 商家商品管理
     * @return 商家商品管理
     */
    @Override
    public List<TGoods> selectTGoodsList(TGoods tGoods)
    {
        return tGoodsMapper.selectTGoodsList(tGoods);
    }

    /**
     * 新增商品
     *
     * @param tGoods 商家商品管理
     * @return 结果
     */
    @Override
    public int insertTGoods(TGoods tGoods)
    {
        String id = IdUtil.getSnowflakeNextIdStr();
        tGoods.setGoodsId(id);
        tGoods.setCreateTime(DateUtils.getNowDate());
        int row = tGoodsMapper.insertTGoods(tGoods);
        if(row>0 && tGoods.gettGoodsSpecsList().size()>0){
            for (TGoodsSpecs tGoodsSpecs:tGoods.gettGoodsSpecsList()) {
                tGoodsSpecs.setGoodsId(tGoods.getGoodsId());
            }
           tGoodsSpecsMapper.insertBatchGoodsSpecs(tGoods.gettGoodsSpecsList());
        }
        return row;
    }

    /**
     * 修改商品
     *
     * @param tGoods 商品管理
     * @return 结果
     */
    @Override
    public int updateTGoods(TGoods tGoods)
    {
        List<TGoodsSpecs> tGoodsSpecsList = tGoodsSpecsMapper.selectTGoodsSpecsByGoodsId(new Long(tGoods.getGoodsId()));
        if(tGoodsSpecsList.size()>0){
            int delRow = tGoodsSpecsMapper.deleteTGoodsSpecsByGoodsId(tGoods.getGoodsId());
            if(delRow>0){
                for (TGoodsSpecs tGoodsSpecs:tGoods.gettGoodsSpecsList()) {
                    tGoodsSpecs.setGoodsId(tGoods.getGoodsId());
                }
                int addSpecs  = tGoodsSpecsMapper.insertBatchGoodsSpecs(tGoods.gettGoodsSpecsList());
                if(addSpecs==0){
                    return addSpecs;
                }
            }else {
                return delRow;
            }
        }else {
            for (TGoodsSpecs tGoodsSpecs:tGoods.gettGoodsSpecsList()) {
                tGoodsSpecs.setGoodsId(tGoods.getGoodsId());
            }
            int addSpecs  = tGoodsSpecsMapper.insertBatchGoodsSpecs(tGoods.gettGoodsSpecsList());
            if(addSpecs==0){
                return addSpecs;
            }
        }
        tGoods.setUpdateTime(DateUtils.getNowDate());
        return tGoodsMapper.updateTGoods(tGoods);
    }

    /**
     * 批量删除商家商品管理
     *
     * @param goodsIds 需要删除的商家商品管理主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsByGoodsIds(Long[] goodsIds)
    {
        return tGoodsMapper.deleteTGoodsByGoodsIds(goodsIds);
    }

    /**
     * 删除商家商品管理信息
     *
     * @param goodsId 商家商品管理主键
     * @return 结果
     */
    @Override
    public int deleteTGoodsByGoodsId(Long goodsId)
    {
        return tGoodsMapper.deleteTGoodsByGoodsId(goodsId);
    }
}
